package org.dtrd.modules.flup.entity.bean;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.dtrd.common.util.DateUtils;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.flup.entity.po.DtrdEntRdFlupAppointmentRecord;

import java.util.Date;

/**
 * @author qr
 * @date 2021/12/23 10:34
 */
@ApiModel("患者随访列表信息")
@Getter
@Setter
public class PatientFlupListInfo {

    @ApiModelProperty("预约记录id")
    private Integer flupAppointmentId;

    @ApiModelProperty("患者id")
    private Integer patientId;

    @ApiModelProperty("机构")
    private String organization;

    @ApiModelProperty("复诊时间")
    private String flupDate;
    @ApiModelProperty("复诊时间段")
    private String flupTimestamp;

    @ApiModelProperty("复诊实际时间")
    private String flupRealDate;

    @ApiModelProperty("复诊状态-文本")
    private String status;

    @ApiModelProperty("复诊状态-数字")
    private Integer flupStatus;

    @ApiModelProperty("医生")
    private String doctorName;

    @ApiModelProperty("接待人")
    private String receiver = "———";

    @ApiModelProperty("接待人")
    private String flupContent;

    @SneakyThrows
    public PatientFlupListInfo parseFromPo(DtrdEntRdFlupAppointmentRecord po) {
        flupAppointmentId = po.getDataId();
        // 复诊状态名称
        Integer flupStatus = po.getFlupStatus();
        Constant.FlupStatus status = Constant.FlupStatus.valueOf(flupStatus);
        assert status != null;
        this.status = status.getName();
        this.flupStatus = flupStatus;
        patientId = po.getPatientId();
        // 设置复诊时间
        Date date = po.getFlupDate();
        Integer time = po.getFlupTime();
        if (date != null && time != null) {
            String dateStr = DateUtils.formatDate(date, DateUtils.date_sdf.get().toPattern());
            Constant.FlupTime constant = Constant.FlupTime.valueOf(time);
            assert constant != null;
            flupDate = dateStr;
            flupTimestamp = constant.getStartTime() + " ~ " + constant.getEndTime();
        }
        // 组成实际的复诊时间段
        Date flupRealStartTime = po.getFlupRealStartTime();
        Date flupRealEndTime = po.getFlupRealEndTime();
        if (flupRealEndTime != null) {
            flupRealDate = wrapperFlupRealTime(flupRealStartTime, flupRealEndTime);
        }
        flupContent = po.getFlupContent();
        return this;
    }

    private String wrapperFlupRealTime(Date startTime, Date endTime) {
        String date = DateUtil.format(startTime, DateUtils.date_sdf.get().toPattern());
        String start = DateUtil.format(startTime, DateUtils.short_time_sdf.get().toPattern());
        String realFlupTime = date + " " + start;
        if (endTime != null) {
            String end = DateUtil.format(endTime, DateUtils.short_time_sdf.get().toPattern());
            realFlupTime = realFlupTime + " ~ "  +  end;
        }
        return realFlupTime;
    }
}
